home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / ctone.zip / TONE.C < prev    next >
Text File  |  1984-12-04  |  1KB  |  39 lines

  1. /* FILENAME: tone.c
  2.  
  3.    DESCRIPTION: generate a tone of given frequency and length
  4.  
  5.  */
  6.  
  7. #define TIMERMODE 182        /* code to put timer in right mode */
  8. #define FREQSCALE 1190000L    /* basic time frequency in hertz   */
  9. #define TIMESCALE 1230L        /* number of counts in 0.1 second  */
  10. #define T_MODEPORT 67        /* port controls timer mode        */
  11. #define FREQPORT   66        /* port controls tone frequency    */
  12. #define BEEPPORT   97        /* port controls speaker           */
  13. #define ON         79        /* signal to turn speaker on       */
  14.  
  15.  
  16. tone (freq, time)
  17.     int freq, time;
  18.     {
  19.  
  20.     int  hibyte, lowbyte, port;
  21.     long i, count, divisor;
  22.  
  23.     divisor = FREQSCALE / freq;    /* scale freq to timer units  */
  24.     lowbyte = divisor % 256; /* break integer into         */
  25.     hibyte  = divisor >> 8;        /*  two bytes                 */
  26.     count   = TIMESCALE * time;    /* convert time to timer units*/
  27.  
  28.     outp (T_MODEPORT, TIMERMODE);    /* prepare timer for input    */
  29.     outp (FREQPORT, lowbyte);    /* set low byte of timer reg  */
  30.     outp (FREQPORT, hibyte); /* set high byte of timer reg */
  31.  
  32.     port = inp (BEEPPORT);        /* save port setting          */
  33.     outp (BEEPPORT, ON);        /* turn speaker on            */
  34.     for (i = 0; i < count; i++)
  35.         ;            /* mark time                  */
  36.     outp (BEEPPORT, port);        /* turn speaker off, restore  */
  37.                  /*  original setting          */
  38.     }
  39.